package com.zpa.redisstreamlistener.handler;

import com.zpa.redisstreamlistener.annotation.RedisStreamListener;
import com.zpa.redisstreamlistener.processor.Acknowledgment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
public class RedisListener {

    /**
     * 负载均衡模式 map
     */
    @RedisStreamListener(topic = "map", groupId = "test-group")
    public void map(Map<String, Object> map) {
        log.info("map 接收到消息：map={}", map);
    }

    /**
     * 负载均衡模式 手动提交 并设置工厂为 redisson2 集群模式（这里是配置的不同bean的地方）
     */
    @RedisStreamListener(topic = "acknowledge", groupId = "test-group", autoSubmit = false, containerFactory = "redisson2")
    public void acknowledge(String json, Acknowledgment acknowledgment) {
        log.info("acknowledge 接收到消息：{}", json);
//        acknowledgment.acknowledge();
    }

    /**
     * 广播模式 groupId
     * 由配置决定 redisson.cluster.stream.broadcast-group-id
     * 配置为空则取随机值用于广播模式
     */
    @RedisStreamListener(topic = "test")
    public void broadcastMessage(Map<String, Object> map) {
        log.info("broadcastMessage 接收到消息：{}", map);
    }
}
